package com.shz.controller;


import com.shz.dto.RespBean;
import com.shz.utils.OSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "图片上传模块")
@RestController
@RequestMapping("/upload")
public class UploadController {
    /**
     * 上传img
     *
     * @param req   要求事情
     * @param image 图像
     * @return {@link RespBean}
     *///    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    @ApiOperation(value = "上传单张图片")
    @ApiImplicitParam(name = "file", value = "file为二进制", required = true)
    @PostMapping("/img")
    public String uploadImg(HttpServletRequest req,@RequestParam("file") MultipartFile image) {
        String s = OSSUtil.uploadImg(image);
        String filePath="https://"+OSSUtil.SUFFER_URL+"/"+s;
        return filePath;
    }


    /**
     * 上传img很多
     *
     * @param req           要求事情
     * @param multipartFile 多部分文件
     * @return {@link List}<{@link String}>
     */
    @ApiOperation(value = "上传多张图片")
    @ApiImplicitParam(name = "file", value = "file为二进制", required = true)
    @PostMapping("/imgMany")
    public List<String> UploadImgMany(HttpServletRequest req,@RequestParam("file") MultipartFile[] multipartFile){
        List<String> urls=new ArrayList<>();
        for (int i = 0; i < multipartFile.length; i++) {
            String s = OSSUtil.uploadImg(multipartFile[i]);
           String filePath="https://"+OSSUtil.SUFFER_URL+"/"+s;
            urls.add(filePath);
        }
        return urls;
    }

}

